package com.qf.gh.dao.impl;

import com.qf.gh.dao.CartDao;
import com.qf.gh.domain.Cart;
import com.qf.gh.utils.DateSourceUtils;
import com.qf.gh.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CartDaoImpl implements CartDao {
    @Override
    public Cart selectByUidAndGid(int id, int parseInt) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "select id,pid,Num,money from tb_cart where id = ? and pid=?";
        try {
            Cart cart=qr.query(sql,new BeanHandler<>(Cart.class),id,parseInt);
            return cart;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询失败");
        }

        return null;
    }

    @Override
    public void add(Cart cart) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "insert into tb_cart (id,pid,Num,money) values (?,?,?,?)";
        try {
            qr.update(sql,cart.getId(), cart.getPid(), cart.getNum(), cart.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增失败");
        }
    }

    @Override
    public void update(Cart cart) {
        QueryRunner qr= new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "UPDATE `tb_cart` SET `Num`=?, `money`=? WHERE (`id`=?) AND (`pid`=?)";
        try {
            qr.update(sql,cart.getNum(), cart.getMoney(), cart.getId(), cart.getPid());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败");
        }
    }

    @Override
    public List<Cart> selectByUid(int id) {
        Connection conn = TxUtils.getConn();
        QueryRunner qr=new QueryRunner();
        String sql = "select id,pid,Num,money from tb_cart where id = ? ";
        try {
            List<Cart> carts=qr.query(conn,sql,new BeanListHandler<>(Cart.class),id);
            return carts;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询购物车失败");
        }

        return null;
    }

    @Override
    public void delect(int id, int parseInt) {
        QueryRunner qr= new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "DELETE FROM tb_cart WHERE (`id`=?) AND (`pid`=?)";
        try {
            qr.update(sql,id,parseInt);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除一条数据失败");
        }
    }

    @Override
    public void clearByUid(int id) {
        QueryRunner qr= new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "DELETE FROM tb_cart WHERE `id`=?";
        try {
            qr.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("清空购物车失败");
        }
    }
}
